package com.example.anotherapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
    private Intent serviceIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btnBindAppService).setOnClickListener(this);
        findViewById(R.id.btnUnbindAppService).setOnClickListener(this);
        serviceIntent = new Intent();
        serviceIntent.setComponent(new ComponentName("com.example.startservicefromanotherapp2","com.example.startservicefromanotherapp2.AppService"));
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btnBindAppService){
            bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
        }
        if(view.getId()==R.id.btnUnbindAppService){
           unbindService(this);
        }
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        System.out.println("Bind Service...");
        System.out.println(iBinder);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {

    }
}
